import { asyncRoutes, constantRouterMap } from '@/router'

const state = {
  refreshMenus:true,
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRouterMap.concat(routes)
  },
  SET_REFRESH_FALSE:(state, routers)=>{
    state.refreshMenus=false;
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      let accessedRoutes
      if (roles.includes('admin')) {
        accessedRoutes = asyncRoutes || []
      } else {
        accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      }
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  },
  setRefreshFasle({ commit }, value){
    commit('SET_REFRESH_FALSE')
  },
  setMenus({ commit }, roles){
    commit('SET_ROUTES', roles)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
